﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;
using YTCMS.Domain.Entities;
using YTCMS.Services.Services;

namespace Admin.Controllers
{
    public class CategoryController : BaseController
    {
        private readonly IcategoryService _category;
        private readonly IproductsService _products;
        public CategoryController(IcategoryService category, IproductsService products)
        {
            this._category = category;
            this._products = products;
        }
        //
        // GET: /Category/
        public ActionResult Index()
        {
            var model = _category.GetAsyncMany(s => s.State == (int)CommonState.正常);
            return View(model);
        }
        [HttpPost]
        public ActionResult Add()
        {
            var id = Request["hidid"].StrConvertInt(0);
            string categoryname = Request["categoryname"];
            int hidpid = Request["hidpid"].StrConvertInt(0).Value;
            int Level = Request["Level"].StrConvertInt(1).Value;
            int ddl_State = Request["ddl_State"].StrConvertInt(0).Value;
            if (string.IsNullOrEmpty(categoryname))
                return new JsonResult() { Data = new { d = false, msg = "请输入类型名称" } };
            var model = _category.GetAsyncMany(s => s.State == (int)CommonState.正常);
            var AncestorsId = GetAncestorsId(hidpid, model, hidpid.ToString());
            ///新增
            if (id == 0)
            {
                var cate = _category.Add(new category() { Name = categoryname, Pid = hidpid, Level = Level, State = ddl_State, CreateTime = System.DateTime.Now, AncestorsId = AncestorsId });
                if (cate.Id > 0)
                    return new JsonResult() { Data = new { d = true }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                return new JsonResult() { Data = new { d = false, msg = "操作失败" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }
            else
            {  //修改
                var vid = id.Value;
                var category = _category.Get(s => s.Id == vid);
                category.Name = categoryname;
                category.State = ddl_State;
                _category.Update(category);
                return new JsonResult() { Data = new { d = true }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }
        }

        public string GetAncestorsId(int id, IEnumerable<category> categorys, string pid)
        {
            pid = id.ToString();
            var parent = categorys.FirstOrDefault(s => s.Id == id);
            if (parent == null)
                return "0";
            if (parent.Pid != 0)
            {
                pid += ("," + parent.Pid);
                GetAncestorsId(parent.Pid.Value, categorys, pid);

            }
            return pid;
        }

        public ActionResult Delete(int id)
        {
            //判断是否有子级分类
            var pid = id.ToString();
            var cs = _category.GetMany(s => s.AncestorsId.Contains(pid));
            if (cs.Count() > 0)
            {
                return new JsonResult() { Data = new { d = false, msg = "该分类下存在子分类，请先删除子分类" } };
            }
            else
            {
                //判断该分类是否关联商品
                var p = _products.GetAsync(s => s.CategoryId == id);
                if (p != null)
                {
                    return new JsonResult() { Data = new { d = false, msg = "该分类下存在商品，请先删除该分类下的所有商品" } };
                }
                var model = _category.Get(s => s.Id == id);
                _category.Delete(model);
                return new JsonResult() { Data = new { d = true, msg = "操作成功" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }
        }
    }
}